package com.cloud.wms.ui.inside.transfer;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.eventBus.Event;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.inside.contract.TransferDownContact;
import com.cloud.wms.ui.inside.entity.TransferCommodityEntity;
import com.cloud.wms.ui.inside.presenter.TransferDownPresenter;
import com.cloud.wms.ui.sort.entity.DetachBoxBean;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * Created by FC on 2021/5/17.
 * Desc：移库上下架选择商品批次
 */
public class TransChooseBatchActivity extends BaseBarListActivity<TransferCommodityEntity, TransferDownPresenter> implements TransferDownContact.View {
    private boolean isDown;
    private DetachBoxBean boxBean;

    @Override
    protected String barTitle() {
        isDown = getIntent().getBooleanExtra(IntentKey.TYPE_KEY, false);
        return isDown ? "移库下架" : "移库上架";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_transfer_down_choose_goods;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_transfer_down_choose_goods;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ArrayList<TransferCommodityEntity> deliveryEntityList = getIntent().getParcelableArrayListExtra(IntentKey.LIST_KEY);
        boxBean = getIntent().getParcelableExtra(IntentKey.OBJECT_KEY);
        mAdapter.setNewInstance(deliveryEntityList);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, TransferCommodityEntity item) {
        helper.setText(R.id.tv_production_date, item.getProductionDate())
                .setText(R.id.tv_expire_date, item.getExpirationDate())
                .setText(R.id.tv_in_date, item.getInDate())
                .setText(R.id.tv_stock_type, item.getStockType() == 1 ? "良品" : "次品")
                .setText(R.id.tv_bar_code, item.getBarCode())
                .setText(R.id.tv_commodity_name, item.getCommodityName())
                .setText(R.id.tv_amount, String.valueOf(item.getAmount()));
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<TransferCommodityEntity, BaseViewHolder> mAdapter, View view, int position) {
        super.itemClickListener(mAdapter, view, position);
        if (isDown)
            TransferDownConfirmActivity.jumpToNext(this, mAdapter.getItem(position), boxBean);
        else
            TransferUpPosActivity.jumpToNext(this, mAdapter.getItem(position));
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventCome(Event event) {
        if (event == null) {
            return;
        }
        switch (event.getCode()) {
            case EventConfig.CLOSE_REFRESH:
                finish();
                break;
        }
    }

    public static void jumpToNext(Activity activity, ArrayList<TransferCommodityEntity> entityList, boolean isDown, DetachBoxBean boxBean) {
        Intent intent = new Intent(activity, TransChooseBatchActivity.class);
        intent.putParcelableArrayListExtra(IntentKey.LIST_KEY, entityList);
        intent.putExtra(IntentKey.TYPE_KEY, isDown);
        intent.putExtra(IntentKey.OBJECT_KEY, boxBean);
        activity.startActivity(intent);
    }

    @Override
    protected void setupComponent() {
    }
}
